<?php
/**
 * Application User Add New
 * This screen has been copied from Orix to insure security
 * separation
 * It will display the application cloud user list
 * 
 * @author Xavier VILAIN
 * @package Hive\Admin
 * @license #license-hive#
 */

use Fox\Html\Button;
use Fox\Behaviors;
use Fox\Html\Block;
use Fox\Html\Screen;
use Fox\Authorization;
use Fox\Generator;
use Fox\Html\Input;
use Fox\Html\DropList;
$xview = Xview::instance();
// Customizing
$customizing = Hive\Admin\Customizing::instance();
$customizing->trackSummaryPage(array('mod' => 'admin','act'=>'users','sac'=>'add'));

if(!Authorization\Manager::check('hive-users-add')) {
	echo $xview->accessDenied(Msg(Hive::BUNDLE,Hive::ERR_ADMIN_ACCES_DENIED),null,true);
	exit;
}
//$userMaxNumber = Hive\License::instance()->check(Hive\License::USER_MAX_NUMBER);

// Page Title
$header = new Fox\Html\Header\Content('page-header',Msg(Hive::BUNDLE,Hive::CREATE_A_NEW_USER));

$screen = new Screen('addUser','create',null,true,Generator::ajaxReload('contentCol'));
$screen->buttons('create')->color(Button::COLOR_WARNING);
$screen->action(Generator::url('admin','users','add'));
$screen->message('<p class="prettyprint">'.Msg(Hive::BUNDLE,Hive::CREATE_A_NEW_USER_HELP).'<p>');
// Block Header
$blHeader = new Fox\Html\Block('bInfo',Msg(Hive::BUNDLE,Hive::GENERAL_INFORMATION));
$blHeader->add(new Input('email',true))->size(Input::SIZE_LARGE);
$blHeader->add(new Input('fname',true));
$blHeader->add(new Input('lname',true));
$blHeader->add(new DropList('proid',true,false,true))
 			->options($xview->application()->profiles(),'proid',Generator::call('label'));// Profile
$screen->add($blHeader);
// Personal Settings
$blLocale = new Fox\Html\Block('bLocale',Msg(Hive::BUNDLE,Hive::LOCALE_SETTINGS));
$blLocale->add(new DropList(array('langu','language'),false,false,true,User::current()->language))
			->options($xview->application()->languages(),'langu',Generator::call('description'));
$blLocale->add(new DropList('dpara',false,true,false,User::current()->dpara));
$blLocale->add(new DropList('npara',false,true,false,User::current()->npara));
$screen->add($blLocale);
//$screen->buttonBarClass('text-center');
// Initialize

if($screen->submitted()) {
	$inputs = $screen->get();
	if(!$screen->errors())
	{
		// Check Duplicates
		$email = $inputs['email'];
		$finder = new User();
		$res = $finder->find(new \Fox\Dao\SqlFragment('LOWER(email) = LOWER(?)',array($email)));
		if (count($res) > 0)
		{
			Fox\Message::Error(Msg(User::BUNDLE_NAME,3,$email));
		}
	}
	if(!$screen->errors()) {
		try {
			$user = new User();
			// Set a default random pass to protect the system
			$inputs['passw'] = md5(Fox\Utilities::buildRandomKey(10));
			// Disable the user
			$inputs['activ'] = false;
			// Set Default Cloud
			$inputs['cloudname'] = User::current()->cloudname();
			$user->setFields($inputs);
			$user->addToActivation(new \Fox\User\Activation());
			$user->create();
			$user->updateWebApps($xview->id(),$inputs['proid']);
			$id = $user->getId();
			// Sent the activation account link to the user
			$link = str_replace('#language#',$inputs['language'],_CLOUD_ACTIVATION_URL_);
			$message = $user->activation()->sendActivationKey(
				Msg(Hive::BUNDLE,Hive::ACTIVATION_EMAIL_TITLE),
				array(Hive::BUNDLE,Hive::ACTIVATION_EMAIL_BODY),
				$link,
				array(Hive::BUNDLE,Hive::EMAIL_TEMPLATE),
				_APPLICATION_DEFAULT_EMAIL_FROM_
			);
			// Attach the current application to the user
			Fox\Message::Success(Msg(Hive::BUNDLE,Hive::CREATE_A_NEW_USER_SUCCESS,$user->email));
		}
		catch (Fox\Object\Exception\Save $e) {
			Fox\Message::Error($e->getMessage());
		}
	}
	return $xview->messages(true,true);
	exit;
}
$html = $screen->display();

$reloadBehavior = new Behaviors(Behaviors::source(Generator::url('admin','users','list')));
?>
<div>
	<div class="page-header clearfix <?php echo $reloadBehavior->serialize(array(),'user-add');?>"><?php echo $header->render();?></div>
	
		
			<?php echo $html;?>
		
	
</div>